package br.com.investmenttracker.controller;

import java.util.List;

import javax.persistence.EntityManager;

import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.Validator;
import br.com.investmenttracker.dao.CategoriaDao;
import br.com.investmenttracker.modelo.Categoria;
import br.com.investmenttracker.modelo.Usuario;

@Resource
public class CategoriaController {
	private Result result;
	private Validator validator;
	private EntityManager em;
	private CategoriaDao dao;

	
	public CategoriaController(EntityManager em,Result result, Validator validator, CategoriaDao dao){
		this.result = result;
		this.validator = validator;
		this.em = em;
		this.dao = dao;
	}
	
	public void form(){
		
	}
	
	public void cadastrar(Categoria categoria){
		Usuario usuario = em.find(Usuario.class, Long.parseLong("2"));
		categoria.setUsuario(usuario);
		em.persist(categoria);
		result.redirectTo(this).listar();
	}
	
	public List<Categoria> listar(){
		result.include("debitos", em.createQuery("Select c from Categoria c where c.tipo='DEBITO'").getResultList());
		result.include("creditos", em.createQuery("Select c from Categoria c where c.tipo='CREDITO'").getResultList());
		return em.createQuery("Select c from Categoria c").getResultList();
	}
	
}
